<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <button id="public">发布动态</button>
    <script>
        // 观察者  粉丝
        class Fan{
            constructor(name){
                this.name = name;
            }
            //接收动态
            update(){
                console.log(`${this.name}接收到变化`)
            }
        }    


        class Star{
            constructor(){
                this.fans = [];
            }
            //添加粉丝
            add(fan){
                this.fans.push(fan);
            }
            //通知粉丝
            public(){
                // [1,2,3].forEach(item =>{})
                this.fans.forEach(fan => {
                    fan.update();
                })
            }
        }

        let star = new Star();

        let f1 = new Fan('小红');
        let f2 = new Fan('小蓝');
        let f3 = new Fan('小绿');
        let f4 = new Fan('小灰');

        star.add(f1);
        star.add(f2);
        star.add(f3);
        star.add(f4);

        let public = document.querySelector('#public');

        public.addEventListener('click',() => {
            star.public();
        })
    </script>
</body>
</html>